﻿$(document).ready(function () {
    $("#pics").cycle({
        fx: 'fade', //Hiệu ứng - Xem thêm các hiệu ứng tại đây: http://jquery.malsup.com/cycle/browser.html
        speed: 1000, //Tốc độ diễn ra hiệu ứng
        timeout: 5000, //3 giây: Thời gian thay đổi giữa các hình 1/1000 giây
        pager: '#slide-nav' //Tạo phân trang cho <div id="slide-nav"></div>
    });


    var $search = $('#searchadvanmargin');
    $('#truot').click(function () {
        if ($search.css('right') == '-10px') {
            $search
                .animate(
                    {
                        'right': '-340px'
                        // to move it towards the right and, probably, off-screen.
                    }, 500
                    //function () {
                    //    $(this).slideUp('fast');
                    //    // once it's finished moving to the right, just 
                    //    // removes the the element from the display, you could use
                    //    // `remove()` instead, or whatever.
                    //}
                    );
            $search.css('right', '-10px');
        }
        else {
            $search
               .animate(
                   {
                       'right': '-10px'
                       // to move it towards the right and, probably, off-screen.
                   }, 500

                   );
        }
    });

    $('#btnLogin').click(function (e) {
        e.preventDefault();
        $.ajax(
            {
                type: "POST",
                url: "/Home/LoginFunc",
                data: $("#formLogin").serialize(),
                dataType: "json",
                success: function (data) {
                    if (data.success == true) {
                       // alert('Login Successfully');
                       window.location = "/Host/Index/" +data.id;
                    } else {
                        $(".validate").css({
                            'display': 'block'
                        });
                    }
                }

            });
    });

    $('#btnRegister').click(function (e) {
        e.preventDefault();
        if (ValidateRegister() == true) {
            $.ajax(
                {
                    type: "POST",
                    url: "/Home/RegisterFunc",
                    data: $("#formRegister").serialize(),
                    dataType: "json",
                    success: function (data) {
                        if (data.check == 1) {
                            $('.validateMail').css({
                                'display': 'block'
                            });
                            $('.validateMail').html("This Email is already taken!");
                        } else {
                            $('.validateMail').css({
                                'display': 'none'
                            });
                        }
                        if (data.check == 2) {
                            $('.validateUsername').css({
                                'display': 'block'
                            });
                            $('.validateUsername').html("This Username is already taken!");
                        } else {
                            $('.validateUsername').css({
                                'display': 'none'
                            });
                        }
                        if (data.success == true) {
                            alert("Now you can login ^^");
                            window.location = "/Home/Index";
                        }
                    }

                });
        }
    });






});

function ValidateRegister() {
    var validmail = "^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}$";
    var checked = true;
    if ($('#inputten').val().length == 0) {
        $('.validateName').css({
            'display': 'block'
        });
        $('.validateName').html("Name must not be empty!");
        checked = false;
    } else {
        $('.validateName').css({
            'display': 'none'
        });
    }

    if ($('#inputemail').val().length == 0) {
        $('.validateMail').css({
            'display': 'block'
        });
        $('.validateMail').html("Email must not be empty!");
        checked = false;
    }
    else if (!$('#inputemail').val().match(validmail)) {
        $('.validateMail').css({
            'display': 'block'
        });
        $('.validateMail').html("Email is invalid!");
        checked = false;
    }
    else {
        $('.validateMail').css({
            'display': 'none'
        });
    }

    if ($('#inputpass').val().length == 0) {
        $('.validatePassword').css({
            'display': 'block'
        });
        $('.validatePassword').html("Password must not be empty!");
        checked = false;
    }
    else if ($('#inputpass').val().length <= 6) {
        $('.validatePassword').css({
            'display': 'block'
        });
        $('.validatePassword').html("Password must contain over 6 letters!");
        checked = false;
    }
    else {
        $('.validatePassword').css({
            'display': 'none'
        });
    }

    if ($('#inputuser').val().length == 0) {
        $('.validateUsername').css({
            'display': 'block'
        });
        $('.validateUsername').html("Username must not be empty!");
        checked = false;
    }
    else {
        $('.validateUsername').css({
            'display': 'none'
        });
    }

    return checked;
}

$('#searchtop').hide();
$(window).scroll(function () {
    if ($(window).scrollTop() > 280) {
        $('#searchtop').fadeIn(500);

    }
    else {
        $('#searchtop').fadeOut(500);
    }

});
